package com.springboot.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.springboot.bean.PropertiesBean;
import com.springboot.constant.ErrorCodeConstants;
import com.springboot.controller.base.BaseController;
import com.springboot.pojo.RequestPojo;
import com.springboot.pojo.ResponsePojo;

@RestController
@RequestMapping("/hello")
public class HelloController extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(HelloController.class);

	@Autowired
	private PropertiesBean propertiesBean;

	@RequestMapping(value = "/say", method = RequestMethod.GET)
	public String sayHello() {
		log.info("SpringApplication start on port : " + propertiesBean.getServerPort());
		return "Hello World on port : " + propertiesBean.getServerPort() + " ,environment : "
				+ propertiesBean.getEnvironment();
	}

	@RequestMapping(value = "/say2/{version}", method = RequestMethod.POST)
	public ResponsePojo<JSONObject> sayHello2(@PathVariable String version, @RequestParam(required = false) String data,
			@ModelAttribute RequestPojo reqPojo, HttpServletRequest request) {
		long st = System.currentTimeMillis();
		log.info("SpringApplication start on port : " + propertiesBean.getServerPort());

		JSONObject body = new JSONObject();
		body.put("mess", "Hello World on port : " + propertiesBean.getServerPort());
		body.put("version", version);
		body.put("data", data);
		body.put("reqPojo", reqPojo);
		body.put("ip", getIpAddr(request));

		return getResponse(ErrorCodeConstants.ERROR_200, st, null, body);
	}

}
